import req from '../utils/request';
import * as url from '../constants/api';

/**
 * @returns { Promise<{ message: string, data: object }> }
 * @description 获取频道列表
 */
export function getChannels () {
    return req({
        url: url.CHANNELS,
        method: 'GET',
    });
}

/**
 * @param { { channel_id: number, timestamp: number } } params
 * @returns { Promise<{ message: string, data: object }> }
 * @description 获取文章列表
 */
export function getArticles (params) {
    return req({
        url: url.ARTICLES,
        method: 'GET',
        params,
    });
}

/**
 * @param { string } keyword
 * @returns { Promise<{ message: string, data: object }> }
 * @description 搜索文章
 */
export function search (keyword) {
    return req({
        url: url.SEARCH,
        method: 'GET',
        params: {
            q: keyword,
        },
    });
}

/**
 * @param { string } keyword
 * @returns { Promise<{ message: string, data: object }> }
 * @description 搜索建议列表
 */
export function getSearchSuggestions (keyword) {
    return req({
        url: url.SEARCH_SUGGESTIONS,
        method: 'GET',
        params: {
            q: keyword,
        },
    });
}

/**
 * @param { string } id
 * @returns { Promise<{ message: string, data: object }> }
 * @description 获取文章详情
 */
export function getArticleDetail (id) {
    return req({
        url: url.ARTICLE_DETAIL + id,
        method: 'GET',
    });
}

/**
 * @param { { type: 'a' | 'c', source: string, offset: string, limit: number } } params
 * @returns { Promise<{ message: string, data: object }> }
 * @description 获取评论列表
 */
export function getCommentList (params) {
    return req({
        url: url.COMMENT_LIST,
        method: 'GET',
        params,
    });
}

/**
 * @param { string } commentId
 * @returns { Promise<{ message: string, data: object }> }
 * @description 点赞评论
 */
export function likeComment (commentId) {
    return req({
        url: url.LIKE_COMMENT,
        method: 'POST',
        data: {
            target: commentId,
        },
    });
}

/**
 * @param { string } commentId
 * @returns { Promise<{ message: string, data: object }> }
 * @description 取消点赞评论
 */
export function dislikeComment (commentId) {
    return req({
        url: `${url.LIKE_COMMENT}/${ commentId }`,
        method: 'DELETE',
    });
}

/**
 * @param { { target: string, content: string, art_id: string } } params
 * @returns { Promise<{ message: string, data: object }> }
 * @description 发表评论
 */
 export function pubComment (params) {
    return req({
        url: url.COMMENT_LIST,
        method: 'POST',
        data: params,
    });
}